package com.ymt.bpm.util;


import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String RegEx util
 * @author Johnny
 *
 */
public class RegExUtil {
	
	private static Logger logger = Logger.getLogger(RegExUtil.class.getName());
	/**
	 * Find variables embraced by {}
	 * @param str
	 * @return
	 */
	public static String[] findEmbracedVars(String str) {
		if (str!=null && str.length()>0) {
			Matcher m = Pattern.compile("\\{[_\\-a-zA-Z][a-zA-Z0-9_]*}").matcher(str);
			List<String> l = new ArrayList<String>();
			while(m.find()){
			    String v = m.group();
			    l.add(v.substring(1, v.length()-1));
			}
			return l.toArray(new String[]{});
		}
		return null;
	}
	
	/**
	 * Find variables embraced by ${}
	 * @param str
	 * @return
	 */
	public static String[] findDollarEmbracedVars(String str) {
		if (str!=null && str.length()>0) {
			Matcher m = Pattern.compile("\\${[_\\-a-zA-Z][a-zA-Z0-9_]*}").matcher(str);
			List<String> l = new ArrayList<String>();
			while(m.find()){
			    String v = m.group();
			    l.add(v.substring(1, v.length()-1));
			}
			return l.toArray(new String[]{});
		}
		return null;
	}

	public static String camelToUnderline(String param){
		String UNDERLINE = "_";
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (Character.isUpperCase(c)){
				sb.append(UNDERLINE);
				sb.append(Character.toUpperCase(c));
			}else{
				sb.append(Character.toUpperCase(c));
			}
		}
		return sb.toString();
	}

	public static String toCamelCase(String s) {
		char UNDERLINE = '_';
		if (s == null) {
			return null;
		}

		s = s.toLowerCase();

		StringBuilder sb = new StringBuilder(s.length());
		boolean upperCase = false;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);

			if (c == UNDERLINE) {
				upperCase = true;
			} else if (upperCase) {
				sb.append(Character.toUpperCase(c));
				upperCase = false;
			} else {
				sb.append(c);
			}
		}

		return sb.toString();
	}

	/**
	 * @param args
	 */
	/*public static void main(String[] args) {
//		Pattern p = Pattern.compile("\\{[_\\-a-zA-Z][a-zA-Z0-9_]*}");
//		Matcher m = p.matcher("aaa{a}bba{3232}{ab}3232");
//		while(m.find()){
//		    logger.finer(m.group());
//		}
//		logger.finer(p.pattern());
//		logger.finer(p.matcher("aaa{a}bba{3232}{ab}3232"));

		String[] matches = findEmbracedVars("aaa{a}bba{3232}{ab}3232");
		for (String a:matches) {
			logger.finer(a);
		}
		
		String[] matches1 = findEmbracedVars("aaa${a}bba${3232}${ab}3232");
		for (String a:matches1) {
			logger.finer(a);
		}

		System.out.println(toCamelCase("D_USER_NAME"));
		
	}*/

}
